<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/15
 */

namespace App\Http\Controllers;

use App\Filters\DictionaryItemsFilter;
use App\Http\Requests\DictionaryItemsRequest;
use App\Http\Resources\DictionaryItemsCollection;
use App\Http\Resources\DictionaryItemsResource;
use App\Models\Dictionary;
use App\Models\DictionaryItems;
use Illuminate\Support\Facades\DB;

/**
 * 数据字典选项模块
 * Class DictionaryItemsController
 * @package App\Http\Controllers
 */
class DictionaryItemsController extends Controller
{

    /**
     * 列表
     * @param Dictionary $dictionary
     * @param DictionaryItemsFilter $filter
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Dictionary $dictionary, DictionaryItemsFilter $filter)
    {
        $paginate = DictionaryItems::filter($filter)->where('did', $dictionary->id)->paginate($this->pageSize());
        $result = DictionaryItemsCollection::make($paginate)->toArray(null);
        return $this->success('', $result);
    }

    /**
     * 添加
     * @param DictionaryItemsRequest $request
     * @param Dictionary $dictionary
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(DictionaryItemsRequest $request, Dictionary $dictionary)
    {
        $data = $request->validate('store');
        $dictionaryItems = new DictionaryItems($data);
        if (!$dictionaryItems->save()) return $this->errorStored();
        return $this->successStored();
    }

    /**
     * 查看
     * @param Dictionary $dictionary
     * @param DictionaryItems $dictionary_item
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(Dictionary $dictionary, DictionaryItems $dictionary_item)
    {
        $resource = DictionaryItems::where(['id' => $dictionary_item->id, 'did' => $dictionary->id])->first();
        if (!$resource) return $this->error(__('common.http_not_found'));
        $result = DictionaryItemsResource::make($resource)->except(['updated_at', 'created_at'])->toArray(null);
        return $this->success('', $result);
    }

    /**
     * 修改
     * @param DictionaryItemsRequest $request
     * @param Dictionary $dictionary
     * @param DictionaryItems $dictionary_item
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(DictionaryItemsRequest $request, Dictionary $dictionary, DictionaryItems $dictionary_item)
    {
        $data = $request->validate('update');
        $result = DictionaryItems::where(['id' => $dictionary_item->id, 'did' => $dictionary->id])->first();
        if (!$result || !$result->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 删除
     * @param Dictionary $dictionary
     * @param $id  // 1或者1,2
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(Dictionary $dictionary, $id)
    {
        DB::beginTransaction();
        try {
            $between = explode(',', $id);
            DictionaryItems::where('did', $dictionary->id)->whereIn('id', $between)->delete();
            DB::commit();
            return $this->successDestoryed();
        } catch (\Exception $e) {
            DB::rollback();
            return $this->exception($e->getMessage())->errorDestoryed();
        }
    }

    /**
     * 更新状态
     * @param DictionaryItemsRequest $request
     * @param Dictionary $dictionary
     * @param DictionaryItems $dictionary_item
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateStatus(DictionaryItemsRequest $request, Dictionary $dictionary, DictionaryItems $dictionary_item)
    {
        $data = $request->validate('updateStatus');
        $result = DictionaryItems::where(['id' => $dictionary_item->id, 'did' => $dictionary->id])->first();
        if (!$result || !$result->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 更新排序
     * @param DictionaryItemsRequest $request
     * @param Dictionary $dictionary
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateSort(DictionaryItemsRequest $request, Dictionary $dictionary)
    {
        $data = $request->validate('updateSort');
        $dictionaryItems = new DictionaryItems();
        $result = $dictionaryItems->batchUpdate($data['sort'], 'id', ' did = ? ', [$dictionary->id]);
        if (!$result) return $this->errorUpdated();
        return $this->successUpdated();
    }

}
